package eu.webmedia.recruitment.comics.preferences;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import eu.webmedia.recruitment.comics.user.User;
import eu.webmedia.recruitment.comics.user.UserUtil;

@Controller
public class PreferenceController {
	
	@Resource
	private PreferenceService preferenceService;
	
	
	@RequestMapping(value = "/preferences.do", method=RequestMethod.GET)
	public String show(Model model) {
		String[] stripNames = { "Cyanide & Happiness", "xkcd", "Wulffmorgenthaler" };
		model.addAttribute("stripNames", stripNames);
		
		User currentUser = UserUtil.getCurrentUser();
		
		Preferences preferences = preferenceService.findByUser(currentUser);
		model.addAttribute("preferences", preferences);
		
		return "preferences";
	}

	@RequestMapping(value = "/preferences.do", method=RequestMethod.POST)
	public String save(Preferences preferences, Model model) {
		User currentUser = UserUtil.getCurrentUser();
		
		preferenceService.save(preferences, currentUser);
		
		return "redirect:/strips.do";
	}

}
